package wnhz.com.business.groupshop;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.KeyEvent;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import wnhz.com.business.BeginActivity;
import wnhz.com.business.MyApplication;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.constants.EXTRA;
import wnhz.com.business.uitls.dialog.UpgradeDialog;
import wnhz.com.business.groupshop.fragment.HomeFragment1;
import wnhz.com.business.groupshop.fragment.HomeFragment2;
import wnhz.com.business.groupshop.fragment.HomeFragment4;
import wnhz.com.business.groupshop.fragment.HomeFragment5;
import wnhz.com.business.uitls.IntentUtils;
import wnhz.com.business.uitls.LogUtils;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.SystemUtils;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;
import wnhz.com.business.view.NoScrollViewPager;


public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener, CompoundButton.OnCheckedChangeListener {

    private NoScrollViewPager vpMain;
    private RadioGroup rgpTab;
    private RadioButton rbtnMain;
    private RadioButton rbtnBusiness;
    private RadioButton rbtnRelease;
    private RadioButton rbtnPerson;

    private String token, mobile, password;
    private Activity activity;
    private UpgradeDialog dialog;
    private long exitTime = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyApplication.getInstance().addActivity(this);
        initView();
        initData();
    }



    private void checkUpdate() {
        Map<String, Object> map = new HashMap<>();
        map.put("type", "2");

        XUtil.Post(Url.INDEX_VERSION, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                LogUtils.e("===版本升级====", result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.optString("re").equals("1")) {
                        if (Integer.valueOf(jsonObject.optString("info")) > SystemUtils.getVersionCode(MainActivity.this)) {
                            dialog = new UpgradeDialog(MainActivity.this, new UpgradeDialog.OnButtonClick() {

                                @Override
                                public void onNegBtnClick() {
                                    IntentUtils.openBrowser(MainActivity.this, "http://a.app.qq.com/o/simple.jsp?pkgname=com.wnhz.guinuo");
                                }
                            });

                            dialog.setCanceledOnTouchOutside(false);
                            dialog.setCancelable(false);
                            dialog.show();
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();

                }
            }
        });

    }

    public void initData() {
        List<Fragment> listFragent = new ArrayList<>();
        HomeFragment1 homeFragment1 = HomeFragment1.newInstance();
        listFragent.add(homeFragment1);
        HomeFragment2 homeFragment2 = HomeFragment2.newInstance();
        listFragent.add(homeFragment2);
        HomeFragment4 homeFragment4 = HomeFragment4.newInstance();
        listFragent.add(homeFragment4);
        HomeFragment5 homeFragment5 = HomeFragment5.newInstance();
        listFragent.add(homeFragment5);

        MainPagerAdapter pageAdapter = new MainPagerAdapter(listFragent);
        vpMain.setAdapter(pageAdapter);
        vpMain.setScroll(true);
        vpMain.setOffscreenPageLimit(0);

        rbtnMain.setOnCheckedChangeListener(this);
        rbtnBusiness.setOnCheckedChangeListener(this);
        rbtnRelease.setOnCheckedChangeListener(this);
        rbtnPerson.setOnCheckedChangeListener(this);
        rgpTab.setOnCheckedChangeListener(this);
        rbtnMain.setChecked(true);
    }

    private void initView() {
        vpMain = (NoScrollViewPager) findViewById(R.id.vp_main);
        rgpTab = (RadioGroup) findViewById(R.id.rgp_tab);
        rbtnMain = (RadioButton) findViewById(R.id.rbtn_main);
        rbtnBusiness = (RadioButton) findViewById(R.id.rbtn_business);
        rbtnRelease = (RadioButton) findViewById(R.id.rbtn_release);
        rbtnPerson = (RadioButton) findViewById(R.id.rbtn_person);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        int currentIndex = intent.getIntExtra(EXTRA.CURRENT_INDEX, 0);
        if (currentIndex != 0 && currentIndex <= 3) {
            vpMain.setCurrentItem(currentIndex, false);
            if (currentIndex == 1) {
                rbtnBusiness.setChecked(true);
            } else if (currentIndex == 2) {
                rbtnRelease.setChecked(true);
            }
        }
    }


    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId) {
            case R.id.rbtn_main:
                vpMain.setCurrentItem(0, false);
                break;
            case R.id.rbtn_business:
                vpMain.setCurrentItem(1, false);
                break;
            case R.id.rbtn_release:
                vpMain.setCurrentItem(2, false);
                break;
            case R.id.rbtn_person:
                vpMain.setCurrentItem(3, false);
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        buttonView.setTextColor(isChecked ? getResources().getColor(R.color.bg_main) : getResources().getColor(R.color.text));
    }

    //内部类 适配器
    class MainPagerAdapter extends FragmentPagerAdapter {
        List<Fragment> fragmentList;

        public MainPagerAdapter(List<Fragment> listist) {
            super(getSupportFragmentManager());
            fragmentList = listist;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            if (fragmentList == null) {
                return 0;
            }
            return fragmentList.size();
        }
    }


    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次后退键将返回商城选择入口", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                new Handler().post(new Runnable() {
                    @Override
                    public void run() {
                        new Toast(getApplicationContext()).cancel();
                    }
                });
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
//                        Intent intent = new Intent(Intent.ACTION_MAIN);
//                        intent.addCategory(Intent.CATEGORY_HOME);
//                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                        startActivity(intent);
//                        MyApplication.getInstance().exit(activity);
                        Intent intent = new Intent(MainActivity.this, BeginActivity.class);
                        startActivity(intent);
                        MyApplication.getInstance().exit(activity);
                    }
                }, 200);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        System.gc();
    }
}
